package net.yunxinyong.cec.utils;

import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import net.yunxinyong.cec.entity.EcnomicInventoryDetailEntity;
import org.springframework.core.io.ClassPathResource;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class DownLoadUtils {

    //文件下载 主要方法
    public static void download(HttpServletResponse response, ArrayList<EcnomicInventoryDetailEntity> list) throws Exception {
        // 指定解析器
        System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
                "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
        //String path = request.getSession().getServletContext().getRealPath("/upload/");
        String filename = "法人和产业活动单位清查表指南.pdf";
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "attachment;fileName="
                + URLEncoder.encode(filename, "UTF-8"));
        OutputStream os = null;
        PdfStamper ps = null;
        PdfReader reader = null;
        try {
            os = response.getOutputStream();
            // 2 读入pdf表单
            reader = new PdfReader("pdfFile" + "/" + filename);
            // 3 根据表单生成一个新的pdf
            ps = new PdfStamper(reader, os);
            // 4 获取pdf表单
            AcroFields form = ps.getAcroFields();
            // 5给表单添加中文字体 这里采用系统字体。不设置的话，中文可能无法显示
            BaseFont bf = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1",
                    BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            form.addSubstitutionFont(bf);
            // 6查询数据================================================
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("social_credit_code", list.get(0).getSocialCreditCode());
            data.put("organization_code", list.get(0).getOrganizationCode());
            data.put("unit_detailed_name", list.get(0).getUnitDetailedName());
            data.put("legal_representative", list.get(0).getLegalRepresentative());
            data.put("unit_location_provience", list.get(0).getUnitLocationProvience());
            data.put("unit_location_city", list.get(0).getUnitLocationCity());
            data.put("unit_location_district", list.get(0).getUnitLocationDistrict());
            data.put("unit_location_town", list.get(0).getUnitLocationTown());

            data.put("unit_location_street", list.get(0).getUnitLocationStreet());
            data.put("unit_loaction_subdistrict", list.get(0).getUnitLoactionSubdistrict());
            data.put("unit_location_cummunity", list.get(0).getUnitLocationCummunity());
            data.put("unit_location_zoning", list.get(0).getUnitLocationZoning());
            data.put("unit_location_villageCode", list.get(0).getUnitLocationVillagecode());
            data.put("unit_location_constructionCode", list.get(0).getUnitLocationConstructioncode());
            data.put("park_location_name", list.get(0).getParkLocationName());
            data.put("park_location_code", list.get(0).getParkLocationCode());
            data.put("unit_registration_province", list.get(0).getUnitRegistrationProvince());
            data.put("unit_registration_city", list.get(0).getUnitRegistrationCity());
            data.put("unit_registration_district", list.get(0).getUnitRegistrationDistrict());
            data.put("unit_registration_town", list.get(0).getUnitRegistrationTown());
            data.put("unit_registration_street", list.get(0).getUnitRegistrationSubdistrict());
            data.put("unit_registration_subdistrict", list.get(0).getUnitRegistrationSubdistrict());
            data.put("unit_registration_cummunity", list.get(0).getUnitRegistrationCummunity());
            data.put("unit_registration_zoning", list.get(0).getUnitRegistrationZoning());
            data.put("park_registration_name", list.get(0).getParkRegistrationName());
            data.put("park_registration_code", list.get(0).getParkRegistrationCode());
            data.put("area_code", list.get(0).getAreaCode());
            data.put("fixed_line_telephone", list.get(0).getFixedLineTelephone());
            data.put("cell_phone_number", list.get(0).getCellPhoneNumber());
            data.put("major_business1", list.get(0).getMajorBusiness1());
            data.put("major_business2", list.get(0).getMajorBusiness2());
            data.put("major_business3", list.get(0).getMajorBusiness3());
            data.put("industry_code", list.get(0).getIndustryCode());
            data.put("industry_name", list.get(0).getIndustryName());
            data.put("verified_major_business1", list.get(0).getVerifiedMajorBusiness1());
            data.put("verified_major_business2", list.get(0).getVerifiedMajorBusiness2());
            data.put("verified_major_business3", list.get(0).getVerifiedMajorBusiness3());
            data.put("verified_industry_code", list.get(0).getVerifiedIndustryCode());
            data.put("verified_industry_name", list.get(0).getVerifiedIndustryName());
            data.put("mechanism_type", list.get(0).getMechanismType());
            data.put("establishment_time", list.get(0).getNewestablishmentTime());
            data.put("business_state", list.get(0).getBusinessState());

            data.put("unit_type",list.get(0).getUnitType());
            data.put("verified_unit_type", list.get(0).getVerifiedUnitType());
            data.put("accounting_standard_category", list.get(0).getAccountingStandardCategory());
            data.put("product_energy", list.get(0).getProductEnergy());
            data.put("sale_energy", list.get(0).getSaleEnergy());
            data.put("is_hava_brances", list.get(0).getIsHavaBrances());
            data.put("count_brances", list.get(0).getCountBrances());
            data.put("legal_unit_type", list.get(0).getLegalUnitType());
            data.put("legal_social_credit_code", list.get(0).getLegalSocialCreditCode());
            data.put("legal_organization_code", list.get(0).getLegalOrganizationCode());
            data.put("legal_unit_detailed_name", list.get(0).getLegalUnitDetailedName());
            data.put("legal_address", list.get(0).getLegalAddress());
            data.put("legal_zoning", list.get(0).getLegalZoning());
            data.put("unique_code", list.get(0).getUniqueCode());
            data.put("data_source", list.get(0).getDataSource());


            // 7遍历data 给pdf表单表格赋值
            for (String key : data.keySet()) {
                System.out.println(data.get(key));
                if (data.get(key) != null &&!data.get(key).equals("")) {
                    form.setField(key, data.get(key).toString());
                }
            }
            ps.setFormFlattening(true);
            //-------------------------------------------------------------
            System.out.println("===============PDF导出成功=============");
        } catch (Exception e) {
            System.out.println("===============PDF导出失败=============");
            e.printStackTrace();
        } finally {
            try {
                ps.close();
                reader.close();
                os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
